package features.advance.ch3;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtils {
    private static final int TIMEOUT_IN_MILLIONS = 6000000;

    public HttpUtils() {
    }

    public static String doGet(String urlStr, Map headParams, Map params) throws Exception {
        URL url = null;
        HttpURLConnection conn = null;
        InputStream is = null;
        BufferedReader reader = null;

        try {
            if (params != null) {
                urlStr = urlStr + "?" + getUrlParamsFromMap(params);
            }

            url = new URL(urlStr);
            conn = (HttpURLConnection)url.openConnection();
            conn.setReadTimeout(6000000);
            conn.setConnectTimeout(6000000);
            conn.setRequestMethod("GET");
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            if (headParams != null) {
                Iterator var8 = headParams.keySet().iterator();

                while(var8.hasNext()) {
                    Object key = var8.next();
                    conn.setRequestProperty((String)key, (String)headParams.get(key));
                }
            }

            StringBuilder response;
            String line;
            if (conn.getResponseCode() != 200) {
                is = conn.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
                response = new StringBuilder();

                while((line = reader.readLine()) != null) {
                    response.append(line);
                }

                throw new RuntimeException(response.toString());
            } else {
                is = conn.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
                response = new StringBuilder();

                while((line = reader.readLine()) != null) {
                    response.append(line);
                }

                String var10 = response.toString();
                return var10;
            }
        } catch (Exception var20) {
            throw new Exception("请求影像服务异常！", var20);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException var19) {
            }

            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException var18) {
            }

            conn.disconnect();
        }
    }

    public static String doPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";

        try {
            URL realUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("charset", "utf-8");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setReadTimeout(6000000);
            conn.setConnectTimeout(6000000);
            if (param != null && !"".equals(param.trim())) {
                out = new PrintWriter(conn.getOutputStream());
                out.print(param);
                out.flush();
            }

            String line;
            for(in = new BufferedReader(new InputStreamReader(conn.getInputStream())); (line = in.readLine()) != null; result = result + line) {
            }
        } catch (Exception var16) {
            var16.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }

                if (in != null) {
                    in.close();
                }
            } catch (IOException var15) {
                var15.printStackTrace();
            }

        }

        return result;
    }

    public static String getUrlParamsFromMap(Map<String, String> map) {
        try {
            if (map != null) {
                StringBuilder stringBuilder = new StringBuilder();
                Iterator var3 = map.entrySet().iterator();

                while(var3.hasNext()) {
                    Entry<String, String> entry = (Entry)var3.next();
                    stringBuilder.append(URLEncoder.encode((String)entry.getKey(), "UTF-8")).append("=").append(URLEncoder.encode((String)entry.getValue(), "UTF-8")).append("&");
                }

                String content = stringBuilder.toString();
                if (content.endsWith("&")) {
                    content = StringUtils.substringBeforeLast(content, "&");
                }

                return content;
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return null;
    }
}
